package com.nexgo.payment.pack;

import com.nexgo.common.ByteUtils;
import com.nexgo.data.entity.SendData;
import com.nexgo.data.entity.SendDataBuilder;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.util.MessageHelper;
import com.nexgo.payment.util.StringUtils;

/**
 * 状态上送
 *
 * Created by xiaox on 2017/8/11.
 */
class UploadStateBuilder extends DataBuilder {
    UploadStateBuilder(TransData transData, DataSource repository) {
        super(transData, repository);
    }

    @Override
    SendData form() {
        SendData sendData = new SendDataBuilder().setListener(mListener).createSendData();
        sendData.setMsgType(mTransData.getTransType().getMsgType());
        sendData.setHeader(MessageHelper.formMessageHeader(mRepository.getParamValue(PreferencesConstants.TPDU)));
        sendData.setTerminalId(mRepository.getParamValue(PreferencesConstants.TERMINAL_ID));
        sendData.setMerchantId(mRepository.getParamValue(PreferencesConstants.MERCHANT_ID));
        sendData.setField60(formField60());
        sendData.setField62(formField62());
        return sendData;
    }


    private String formField62() {
        int i = 0;
        StringBuilder sb = new StringBuilder();
        // 1-5为 硬件状态 ，0x51 通讯统计 ，其他为下载的参数
        for (i = 0x01; i < 0x28; i++) {
            switch (i) {
                case 0x01:                                                                                                    //硬件状态
                case 0x02:
                case 0x03:
                case 0x04:
                case 0x05:
                    sb.append(String.format(SysConstant.LOCALE, "%02d", i));
                    sb.append("1");
                    break;
                case 0x11:                                                                                                    //POS终端应用类型
                    sb.append(String.format(SysConstant.LOCALE, "%02d", i));
                    sb.append("60");
                    break;
                case 0x12:                                                                                                    //超时时间
                    sb.append(String.format(SysConstant.LOCALE, "%02d", i));
                    sb.append(StringUtils.leftPad(StringUtils.firstNonEmpty(mRepository.getParamValue(PreferencesConstants.COMMUNICATION_TIMEOUT), "0"), 2, '0'));
                    break;
                case 0x13:                                                                                                    //重拨次数
                    sb.append(String.format(SysConstant.LOCALE, "%02d", i));
                    sb.append("0");
                    break;
                case 0x14:                                                                                                    //电话号码1
                case 0x15:                                                                                                    //电话号码2
                case 0x16:                                                                                                    //电话号码3
                case 0x17:                                                                                                    //管理电话
                    sb.append(String.format(SysConstant.LOCALE, "%02d", i));
                    sb.append(StringUtils.leftPad("", 14, ' '));
                    break;
                case 0x18:                                                                                                    //是否支持小费
                    sb.append(String.format(SysConstant.LOCALE, "%02d", i));
                    sb.append("0");
                    break;
                case 0x19:                                                                                                    //小费百分比
                    sb.append(String.format(SysConstant.LOCALE, "%02d", i));
                    sb.append("00");
                    break;
                case 0x20:                                                                                                    //是否支持手工输入卡号
                    sb.append(String.format(SysConstant.LOCALE, "%02d", i));
                    sb.append(StringUtils.firstNonEmpty(mRepository.getParamValue(PreferencesConstants.ALLOW_MANUAL_PAN), "0"));
                    break;
                case 0x21:                                                                                                    //是否自动签退
                    sb.append(String.format(SysConstant.LOCALE, "%02d", i));
                    sb.append(StringUtils.firstNonEmpty(mRepository.getParamValue(PreferencesConstants.AUTO_LOGOUT_SWITCH), "0"));
                    break;
                case 0x23:                                                                                                    //交易重发次数
                    sb.append(String.format(SysConstant.LOCALE, "%02d", i));
                    sb.append(StringUtils.firstNonEmpty(mRepository.getParamValue(PreferencesConstants.REVERSAL_UPLOAD_TIMES), "0"));
                    break;
                case 0x24:                                        //离线交易上送方式 0:批结算之前一并上送 1:随下笔联机交易上?
                    sb.append(String.format(SysConstant.LOCALE, "%02d", i));
                    sb.append("0");
                    break;
                case 0x25:                                                                                                    //主密钥INDEX
                    sb.append(String.format(SysConstant.LOCALE, "%02d", i));
                    sb.append(StringUtils.firstNonEmpty(mRepository.getParamValue(PreferencesConstants.MASTER_KEY_INDEX), "0"));
                    break;
                case 0x27:                       //终端累积自动上送笔数
                    sb.append(String.format(SysConstant.LOCALE, "%02d", i));
                    sb.append(StringUtils.leftPad(mRepository.getParamValue(PreferencesConstants.OFFLINE_TRANS_UPLOAD_NUMBER), 2, '0'));
                    break;
                default:
                    break;
            }
        }
        // POS拨通率 N12(N4+N5+N3)
        sb.append("51");
        // 4位从上次成功统计到本次统计的时间段(小时)+5位数字表示的总拨号次数+3位数字表示的拨通率(百分比)
        sb.append(StringUtils.leftPad("", 4, '0'));
        sb.append(StringUtils.leftPad("", 5, '0'));
        sb.append("100");
        return ByteUtils.byteArray2HexString(sb.toString().getBytes());
    }

    private String formField60() {
        StringBuilder sb = new StringBuilder();
        sb.append(mTransData.getTransType().getFuncCode());
        sb.append(mRepository.getParamValue(PreferencesConstants.BATCH_NUMBER));
        sb.append("362");
        return ByteUtils.byteArray2HexString(sb.toString().getBytes());
    }
}
